﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Bing;

namespace WindowsFormsBing
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            AddItem(cboFrom);
            AddItem(cboTo);
        }

        string AppId = "B692A148D1624C4E3C1248C8E5DDC209E524D2C4";

        private void btnTranslate_Click(object sender, EventArgs e)
        {
            string query = txtQuery.Text.Trim();
            this.txtResult.Text = String.Empty;

            if (string.IsNullOrEmpty(query))
            {
                MessageBox.Show("請輸入要翻譯的文字");
                return;
            }

            if (this.cboFrom.SelectedIndex < 0)
            {
                MessageBox.Show("請選擇來源語系");
                return;
            }

            if (this.cboTo.SelectedIndex < 0)
            {
                MessageBox.Show("請選擇目的語系");
                return;
            }

            SearchRequest searchRequest = new SearchRequest() { AppId = AppId, Query = query, Market = "en-US" };

            TranslationRequest translationRequest = new TranslationRequest();  // 宣告 TranslationRequest 並實例化
            translationRequest.SourceLanguage = cboFrom.Text.ToString();  // 來源語系
            translationRequest.TargetLanguage = cboTo.Text.ToString();  // 目的語系

            TranslationResponse response = API.Translation(searchRequest, translationRequest);

            if (response.TranslationResults.Count > 0)  // 翻譯回傳值大於0時
            {
                txtResult.Text = response.TranslationResults[0].TranslatedTerm;
            }
        }

        public void AddItem(ComboBox cbo)
        {
            cbo.Items.Add("Ar");    // Arabic
            cbo.Items.Add("zh-CHS");// Simplified Chinese
            cbo.Items.Add("zh-CHT");// Traditional Chinese
            cbo.Items.Add("Nl");    // Dutch
            cbo.Items.Add("En");    // English
            cbo.Items.Add("Fr");    // French
            cbo.Items.Add("De");    // German
            cbo.Items.Add("It");    // Italian
            cbo.Items.Add("Ja");    // Japanese
            cbo.Items.Add("Ko");    // Korean
            cbo.Items.Add("Pl");    // Polish
            cbo.Items.Add("Pt");    // Portuguese
            cbo.Items.Add("Ru");    // Russian
            cbo.Items.Add("Es");    //"Spanish
        }
    }
}
